package br.com.dallas.util.menu;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.dallas.usuario.javabean.UsuarioBean;

@ManagedBean(name="menuManagerBean")
@RequestScoped
public class MenuManagerBean
{
	private static final int GERENCIAMENTO_VOLUMES = 1;
	private static final int INSERIR_USUARIO = 2;
	private static final int ALTERAR_USUARIO = 3;
	private static final int ALTERAR_PARAMETRO = 4;
	private static final int GERENCIAMENTO_TRANSPORTADORAS = 5;
	
	private static final int ALTERAR_SENHA = 20;
	private static final int AJUDA_CLIENTE = 21;
	private static final int SAIR = 22;

	public void requisicaoMenu(int requisicao)
	{
		HttpServletRequest request = null;
		HttpSession session = null;
		UsuarioBean usuarioBean = null;
		try
		{
			request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
			session = (HttpSession) request.getSession();
			usuarioBean = (UsuarioBean)session.getAttribute("usuarioLogado");
			
			if (usuarioBean == null)
			{
				FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/dispatcher/Sessao.jsf");
				return;
			}
			
			switch (requisicao)
			{
				case GERENCIAMENTO_VOLUMES:
				{
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/trans/Transportadoras.jsf");
					break;
				}
				case INSERIR_USUARIO:
				{
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/usuario/IncluirUsuario.jsf");
					break;
				}
				case ALTERAR_USUARIO:
				{
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/usuario/AlterarUsuario.jsf");
					break;
				}
				case ALTERAR_SENHA:
				{
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/usuario/UsuarioAlterarSenha.jsf");
					break;
				}
				case ALTERAR_PARAMETRO:
				{
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/parametro/Parametro.jsf");
					break;
				}
				case AJUDA_CLIENTE:
				{
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/dispatcher/HelpSistema.jsf");
					break;
				}
				case GERENCIAMENTO_TRANSPORTADORAS:
				{
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/trans/ManagementCarriers.jsf");
					break;
				}
				case SAIR:
				{
					session.removeAttribute("usuarioLogado");
					session.removeAttribute("parametroLogado");
					FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/");
					break;
				}
				default:
					break;
			}
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}
